package logmanager.view;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

import java.awt.*;

//claase effectuant le coloriage des lignes
public class ColorCellRenderer extends DefaultTableCellRenderer {
	private static final long serialVersionUID = -2239923756881127341L;
	
	private int[] lines = null;
	private Color color = null;
	private boolean apply, alt_show;

	public ColorCellRenderer(int[] lines, Color color, boolean apply, boolean alt_show) {
		this.lines = lines;
		this.color = color;
		this.apply = apply;
		this.alt_show = alt_show;
	}
	@Override
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
		super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
		//prepareRenderer(table, table.getCellRenderer(row, column), row, column);
		Color[] colors = {new Color(250,250,250), new Color(220,220,220)};
		Color colorss = getBackground();
		if(contain(row) && apply)
			setBackground(color);
		else{
			if ( !table.isCellSelected(row, column)) {
				colorss = colors[row % colors.length];
			}
			if(!alt_show)
				setBackground(Color.WHITE);
			else
				setBackground(colorss);
		}
		return this;
	}
	
	public Component prepareRenderer(JTable table, TableCellRenderer renderer, int row,
			int column) {
		Color[] colors = {new Color(250,250,250), new Color(240,240,240)};
		Component c = table.prepareRenderer(renderer, row, column);
		Color color = getBackground();
		if ( !table.isCellSelected(row, column)) {
			color = colors[row % colors.length];
		}
		c.setBackground(color);
		return c;
	}

	private boolean contain(int row){
		for (int i = 0; i < lines.length; i++) {
			if(lines[i] == row)
				return true;
		}
		return false;
	}
	public int[] getLines() {
		return lines;
	}
	public void setLines(int[] lines) {
		this.lines = lines;
	}
	public Color getColor() {
		return color;
	}
	public void setColor(Color color) {
		this.color = color;
	}
	public boolean isApply() {
		return apply;
	}
	public void setApply(boolean apply) {
		this.apply = apply;
	}
}